<?php
namespace app\ausadmin\controller;
use app\ausadmin\controller\Common;
use Godok\Org\Adminauth;
use think\Controller;
use think\Request;
use think\Db;
use think\File;
use think\Loader;
use think\Config;
class Ftp extends Common
{
	public function index(){
		if($_FILES['file']['name']){
			$path = 'uploads/ota/';
			$file = Request::instance()->file('file');
			$file_info = $file->move($path,'');
			$filename = $file_info->getSaveName();
			$source = ROOT_PATH . 'public' . DS . 'uploads'.DS.'ota'.DS.$file_info->getSaveName();//本地文件
			$FTP_HOST = config('ftp.addr');//ftp服务器地址
			$FTP_PORT = config('ftp.port');  //ftp端口
		    $FTP_USER = config('ftp.username');//ftp用户名
		    $FTP_PASS = config('ftp.password');//ftp用户密码
			$conn = ftp_connect($FTP_HOST,$FTP_PORT)  or die("Could not connect"); // 联接FTP服务器 	
			ftp_login($conn, $FTP_USER,$FTP_PASS); // 使用username和password登录 
			ftp_pasv($conn,TRUE);//
			if(ftp_put($conn,"$filename","$source",FTP_ASCII)){
				$data['file_name'] = $filename;
				$data['create_time'] = time();
				$data['create_user'] = session('adminid');
				$data['is_delete'] = 1;
				Db::name('charecabinet_version')->insert($data);
				unlink($source);//删除文件
				$oid = 0;$m = 'ausadmin'; $c = 'Ftp'; $action="index"; $uid=session('adminid'); $querystring = '上传机柜版本文件'.$filename;
				$ip = get_ip();$os = get_os();
				set_logs($oid,$m,$c,$action,$uid,$querystring,$ip,$os);
				ftp_quit($conn);//关闭连接
				return ['code'=>0,'msg'=>lang('上传成功！')];
			} else{
				return ['code'=>-1050,'msg'=>lang('上传失败！')];
			}
		}
	}
}